package cn.littlehx.simple_score_manage.service.impl;

import cn.littlehx.simple_score_manage.common.MyUtils;
import cn.littlehx.simple_score_manage.dao.ICourseDao;
import cn.littlehx.simple_score_manage.dao.IScoreDao;
import cn.littlehx.simple_score_manage.dao.ITeacherDao;
import cn.littlehx.simple_score_manage.domain.Course;
import cn.littlehx.simple_score_manage.domain.Score;
import cn.littlehx.simple_score_manage.domain.Teacher;
import cn.littlehx.simple_score_manage.service.ITeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class TeacherServiceImpl extends ServiceImpl<ITeacherDao, Teacher> implements ITeacherService {
    @Autowired
    private ICourseDao courseDao;
    @Autowired
    private IScoreDao scoreDao;
    @Autowired
    private ITeacherDao teacherDao;

    @Override
    public List<Map<String, String>> selectScoreByCourseNo(String courseNo) {
        return teacherDao.selectScoreByCourseNo(courseNo);
    }

    @Override
    public List<Course> selectCourseByTeacherNo(String teacherNo) {
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Course::getTeacherNo,teacherNo);
        return courseDao.selectList(wrapper);
    }

    @Override
    public boolean save(Teacher entity) {
        if (entity.getPassword() != null) {
            //增加MD5加密
            entity.setPassword(MyUtils.encode(entity.getPassword()));
        } else {
            //增加默认密码000000并进行加密
            //增加MD5加密
            entity.setPassword(MyUtils.encode("000000"));
        }
        return super.save(entity);
    }

    @Override
    public boolean updateById(Teacher entity) {
        if (entity.getPassword() != null&&!entity.getPassword().equals("")) {
            //增加MD5加密
            entity.setPassword(MyUtils.encode(entity.getPassword()));
//            log.info("进入加密"+entity.getPassword());
        }else{
            entity.setPassword(null);
//            log.info("进入else,结果为:"+entity.getPassword());
        }
//        log.info(entity.toString());
        return super.updateById(entity);
    }

    @Override
    public Teacher selectByNoAndPassword(String no, String password) {
        //        对密码进行加密
        password = MyUtils.encode(password);
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teacher::getNo, no);
        wrapper.eq(Teacher::getPassword, password);
        List<Teacher> list = this.list(wrapper);
        return list.get(0);
    }

    @Override
    public Teacher selectByNoAndPasswordAdmin(String no, String password) {
        //        对密码进行加密
        password = MyUtils.encode(password);
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teacher::getNo, no);
        wrapper.eq(Teacher::getPassword, password);
        wrapper.eq(Teacher::getPriority,1);
        List<Teacher> list = this.list(wrapper);
        return list.get(0);
    }
}
